package com.jiazimall.commodity.service.impl;

//import com.aliyun.oss.ClientBuilderConfiguration;
//import com.aliyun.oss.OSS;
//import com.aliyun.oss.OSSClientBuilder;
//import com.aliyun.oss.common.auth.CredentialsProviderFactory;
//import com.aliyun.oss.common.comm.SignVersion;
import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSSClient;
import com.jiazimall.commodity.mapper.CategoryBrandRelationDao;
import com.jiazimall.commodity.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wujiahao.common.utils.PageUtils;
import com.wujiahao.common.utils.R;
import com.wujiahao.common.utils.Query;

import com.jiazimall.commodity.mapper.BrandDao;
import com.jiazimall.commodity.pojo.BrandEntity;
import com.jiazimall.commodity.service.BrandService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
    @Resource
    private OSSClient ossClient;
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String)params.get("key");
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(key)){
            wrapper.eq("brand_id",key).or().like("name",key);
        }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }
    @Override
    public void uploadOSS(){
        try {
            // 填写Bucket名称，例如examplebucket。
            String bucketName = "jiazimall";
            // 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。
            String objectName = "oppo.png";
            // 填写本地文件的完整路径，例如D:\\localpath\\examplefile.txt。
            // 如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件。
            String filePath= "E:\\prcture\\oppo.png";
            //上传文件流
            InputStream inputStream = new FileInputStream(filePath);
            ossClient.putObject(bucketName,objectName,inputStream);
            //关闭ossClient
            ossClient.shutdown();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("上传成功！");
    }
    @Transactional
    @Override
    public void updateDetail(BrandEntity brand) {
        //保证冗余字段的数据一致性
        this.updateById(brand);
        if (StrUtil.isNotBlank(brand.getName())){
            //同步更新至其他关联表
            categoryBrandRelationService.updateBrandName(brand.getBrandId(),brand.getName());
        }
        // TODO: 2024/11/25 更新其他关联
    }
}